/*
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx;


/**
 *
 * @author jdp
 */
public class Table
    extends Container
{

    protected int cellspacing, cellpadding;


    public Table(){
        super();
    }


    public void layout(){
        super.layout();
    }
    public int getCellspacing(){
        return this.cellspacing;
    }
    public void setCellspacing(int value){
        this.cellspacing = value;
    }
    public void setCellspacing(String value){
        this.setCellspacing(Integer.parseInt(value));
    }
    public int getCellpadding(){
        return this.cellpadding;
    }
    public void setCellpadding(int value){
        this.cellpadding = value;
    }
    public void setCellpadding(String value){
        this.setCellpadding(Integer.parseInt(value));
    }
    public int getRows(){
        int rows = 0;
        for (Component child : this){
            if (child instanceof Tr)
                rows += 1;
        }
        return rows;
    }
    public int getColumns(){
        int columns = 0;
        for (Component child : this){
            if (child instanceof Tr)
                columns = Math.max(columns,((Tr)child).countColumns());
        }
        return columns;
    }
    public void endElement(){
        int space = this.cellspacing;
        if (0 < space){
            for (Component child : this){
                child.setMargin(space);
            }
        }
        int pad = this.cellpadding;
        if (0 < space){
            for (Component child : this){
                child.setPadding(pad);
            }
        }
        float columns = this.getColumns();
        if (0f < columns){
            columns = (1f/columns)*100f;
            String rs = String.valueOf(columns)+'%';

            for (Component child : this){
                if (child instanceof Tr){
                    child.setRelative("height",rs);
                }
            }
        }
        float rows = this.getRows();
        if (0f < rows){
            rows = (1f/rows)*100f;
            String rs = String.valueOf(rows)+'%';

            for (Component child : this){
                if (child instanceof Tr){
                    ((Tr)child).endElementTableRows(rows,rs);
                }
            }
        }
    }

}
